package com.panshi.controller;

import com.panshi.entity.SysAnswerCheck;
import com.panshi.service.AnswerCheckService;
import com.panshi.tools.DataResult;
import com.panshi.tools.PageBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 回答未审核controller
 */
@RestController
@Api(tags = "回答未审核业务接口")
public class AnswerCheckController {

    @Autowired
    private AnswerCheckService answerCheckService;

    @Autowired
    private HttpServletRequest request;

    @ApiOperation("增加回答")
    @ApiImplicitParam(name = "sysAnswerCheck", value = "未审核回答对象", required = true)
    @PostMapping("answer/add")
    public DataResult add(@RequestBody  SysAnswerCheck sysAnswerCheck){
       return  answerCheckService.addAnswer(sysAnswerCheck);
    }

    @ApiOperation("修改回答")
    @ApiImplicitParam(name = "sysAnswerCheck", value = "未审核回答对象", required = true)
    @PostMapping("answer/update")
    public DataResult update(@RequestBody SysAnswerCheck sysAnswerCheck){
        return answerCheckService.updateAnswer(sysAnswerCheck);
    }

    @ApiOperation("删除回答")
    @ApiImplicitParam(name = "answerId", value = "回答id", required = true)
    @GetMapping("answer/delete/{answerId}")
    public DataResult delete(@PathVariable Integer answerId){
        return answerCheckService.deleteAnswer(answerId);
    }

    @ApiOperation("执行审核")
    @ApiImplicitParam(name = "sysAnswerCheck", value = "未审核回答对象", required = true)
    @PostMapping("answer/adopt")
    public DataResult adopt(SysAnswerCheck sysAnswerCheck){
        return answerCheckService.adoptCheck(sysAnswerCheck);
    }

    @ApiOperation("根据用户id查询")
    @ApiImplicitParam(name = "userId", value = "用户id", required = true)
    @GetMapping("answer/user/{userId}") // http://localhost:1002/answer/user/2?pageNumber=1&pageSize=10
   // @HystrixCommand(fallbackMethod = "queryGet")
    public DataResult queryByUid(@PathVariable Integer userId, PageBean pageBean){
        DataResult data = answerCheckService.queryByUid(userId,pageBean);
        if(null == data.getData()){
            throw new RuntimeException();
        }
        return data;
    }

    @ApiOperation("查询全部回答")
    @GetMapping("answer/query")
    public DataResult query(){
        PageBean pageBean = new PageBean();
        pageBean.setPageNumber(Integer.valueOf(request.getParameter("page")));
        pageBean.setPageSize(Integer.valueOf(request.getParameter("limit")));
        return answerCheckService.query(null,pageBean);
    }

    @ApiOperation("查询未审核回答")
    @GetMapping("answer/uncheck")
    public DataResult queryUnCheck(){
        PageBean pageBean = new PageBean();
        pageBean.setPageNumber(Integer.valueOf(request.getParameter("page")));
        pageBean.setPageSize(Integer.valueOf(request.getParameter("limit")));
        return answerCheckService.query(2,pageBean);
    }

    // 熔断方法
    public DataResult queryGet(@PathVariable Integer userId, PageBean pageBean){
        return new DataResult().setCode(500).setMsg("用户id => " + userId +" 不存在，没有对应的数据，请重试~" );
    }

}
